package com.yuerlive.rainbow.module.search.presenter;

import com.yuerlive.rainbow.api.SearchApi;
import com.yuerlive.rainbow.api.SearchService;
import com.yuerlive.rainbow.api.SubscribeApi;
import com.yuerlive.rainbow.api.SubscribeService;
import com.yuerlive.rainbow.api.UserApi;
import com.yuerlive.rainbow.api.UserService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.Anchor;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.SearchData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/5/24.
 */

public class SearchPresenter extends BasePresenterImpl implements SearchContract.Presenter {
    private SearchContract.View view;
    private SearchService searchService;
    private SubscribeService subscribeService;
    private UserService userService;

    public SearchPresenter(SearchContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        searchService = SearchApi.getInstance().service;
        subscribeService = SubscribeApi.getInstance().service;
        userService = UserApi.getInstance().service;
    }

    @Override
    public void getOthersSearch() {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();

        searchService.getOthersSearch(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<Anchor>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<Anchor>> listBaseData) {
                        view.hideLoading();
                        view.showOthersSearch(listBaseData.getObject());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void search(String key) {
        User user = UserProxy.getUser();
        Map<String, String> params = new HashMap<>();

        params.put("keyword", key);
        params.put("type", "1");
        searchService.search(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<SearchData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<SearchData> listBaseData) {
                        view.showSearchResults(listBaseData.getObject().getUp().getList());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void subscribeUp(String upId, int pos) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();

        params.put("upUserId", upId);

        subscribeService.subscribe(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.hideLoading();
                        view.showSubscribeResult(baseData.getCode(), pos);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getUserInfo(String userId, int pos) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        params.put("otherId", userId);

        userService.getUserInfo(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<UserInfo>>(this) {
                    @Override
                    protected void onSuccess(BaseData<UserInfo> anchorBaseData) {
                        view.hideLoading();
                        view.showUserInfo(anchorBaseData, pos);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }


}
